#include "ProductWatermark.h"
#include "log/log.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <log/log.h>
#include <iostream>
#include <stdio.h>
#include <cstring>
namespace android
{

    ProductWatermark::ProductWatermark(WatermarkManager *manager, BufferQueue<WMarkBuffer> *queue) : ReaderThread(manager, queue)
    {
        mFtype = new FreetypeWrapper();
        //testSpeed();
    }

    ProductWatermark::~ProductWatermark()
    {
        if (mFtype)
        {
            delete mFtype;
            mFtype = nullptr;
        }
    }

    int ProductWatermark::recvMsg(WMarkBuffer *buf)
    {
        if (mFtype)
        {
            int h = buf->getDataHeight();
            int w = buf->getDataWidth();
            ALOGV("h=%d w=%d\n", h, w);
            uint8_t **data = buf->Data();
            for (int i = 0; i < h; i++) {
                memset(*(data + i), 0, w);
            }

            DriverStrInfo strInfo = WMarkStr::getInstance()->getStrInfo();

            buf->displayX = strInfo.mDisplayX;
            buf->displayY = strInfo.mDisplayY;
            buf->cameraId = strInfo.mCameraID;
            buf->enableWmark = strInfo.mEnable;

            mFtype->text2img(strInfo.mStr, buf->Data(), buf->getDataWidth(), buf->getDataHeight());
        }
        //TODO read data to buf->Data()

        return buf->getSize();
    }

    void ProductWatermark::formatDateTime(time_t seconds, char *buffer, int bufferLength)
    {
        struct tm tm;

        localtime_r(&seconds, &tm);
        snprintf(buffer, bufferLength, "%04d-%02d-%02d %02d:%02d:%02d",
                 tm.tm_year + 1900,
                 tm.tm_mon + 1, // localtime_r uses months in 0 - 11 range
                 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
    }

}
